﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookManage
{
    public partial class User2 : Form
    {
        public User2()
        {
            InitializeComponent();
            Table();
        }
        private void Table()
        {
            dataGridView1.Rows.Clear();
            Dao dao = new Dao();
            string sql = "select * from t_book;";
            IDataReader dc = dao.read(sql);

            while (dc.Read())
            {
                dataGridView1.Rows.Add(dc["id"].ToString(), dc["name"].ToString(), dc["author"].ToString(), dc["press"].ToString(), dc["number"].ToString());
            }
            dc.Close();
            dao.DaoClose();
        }
        private void User2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            int number = int.Parse(dataGridView1.SelectedRows[0].Cells[4].Value.ToString());
            if(number<1)
            {
                MessageBox.Show("库存不足，请联系管理员");
                return;
            }

            string sql = $"insert into t_lend([uid],bid,[datetime]) values('{Data.UID}','{id}',getdate());update t_book set number = number - 1 where id = '{id}'; ";
            Dao dao = new Dao();
            if(dao.Execute(sql)>1)
            {
                MessageBox.Show("借书成功");
                Table();
            }
            else
            {
                MessageBox.Show("借书失败");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            string sql = $"select * from t_book where id = '{textBox1.Text}';";
            Dao dao = new Dao();
            IDataReader dc = dao.read(sql);
            while (dc.Read())
            {
                dataGridView1.Rows.Add(dc["id"].ToString(), dc["name"].ToString(), dc["author"].ToString(), dc["press"].ToString(), dc["number"].ToString());
            }
            dc.Close();
            dao.DaoClose();
            textBox1.Text = "";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            string sql = $"select * from t_book where name like '%{textBox2.Text}%';";
            Dao dao = new Dao();
            IDataReader dc = dao.read(sql);
            while (dc.Read())
            {
                dataGridView1.Rows.Add(dc["id"].ToString(), dc["name"].ToString(), dc["author"].ToString(), dc["press"].ToString(), dc["number"].ToString());
            }
            dc.Close();
            dao.DaoClose();
            textBox2.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Table();
        }
    }
}
